﻿namespace ETestIT.UI
{
    using System;
    using System.Web.UI.WebControls;
    using Core;
    using Core.Storage;
    using Domain.Entities;
    using Domain.Repositories;
    using Domain.Services;
    using Presentation;
    using StructureMap;

    public partial class Resumes : PageBase<Resumes>
    {
        ISessionStorage sessionStorage;
        IResumeRepository resumeRepository;
        ICandidateResumeService candidateResumeService;
        ICadidateRepository cadidateRepository;

        protected void Page_Load(object sender, EventArgs e)
        {
            sessionStorage = ObjectFactory.GetInstance<ISessionStorage>();
            resumeRepository = ObjectFactory.GetInstance<IResumeRepository>();
            cadidateRepository = ObjectFactory.GetInstance<ICadidateRepository>();
            candidateResumeService = ObjectFactory.GetInstance<ICandidateResumeService>();

            resumeAddUpdate.Update += ResumeAddUpdateUpdate;

            if(!IsPostBack)
            {
                if (sessionStorage.Contains(Const.SelectedCandidateKey))
                    LoadCandidateResumes();
            }
        }

        private void LoadCandidateResumes()
        {
            var candidateId = sessionStorage.Get<Guid>(Const.SelectedCandidateKey);

            gvReumes.DataSource = candidateResumeService.GetLatestVersionsResume(candidateId);
            gvReumes.DataBind();
        }

        protected void LnkResumesViewCommand(object sender, CommandEventArgs e)
        {
            var resumeId = new Guid(e.CommandArgument.ToString());

            switch (e.CommandName)
            {
                case "ViewCandidateResume":
                    {
                        var resume = resumeRepository.GetById(resumeId);
                        Response.WriteResumeToResponse(resume);
                        break;    
                    }
                case "UpdateCandidateResume" :
                    {
                        var resume = resumeRepository.GetById(resumeId);
                        sessionStorage.Set(Const.SelectedResumeKey, resumeId);
                        resumeAddUpdate.SetModel(resume);
                        resumeAddUpdate.Visible = true;
                        break;
                    }    
                case "RemoveCandidateResume" :
                    {
                        candidateResumeService.Delete(resumeId);
                        break;
                    }    
            }

            LoadCandidateResumes();
        }

        protected void BtnAddNewResume_Clicked(object sender, EventArgs e)
        {
            resumeAddUpdate.Visible = true;
        }

        public void ResumeAddUpdateUpdate(object sender, EventArgs e)
        {
            Resume resume = null;
            Guid updatedResumeId = Guid.Empty;

            var isUpdate = sessionStorage.Contains(Const.SelectedResumeKey);

            if (isUpdate)
            {
                updatedResumeId = sessionStorage.Get<Guid>(Const.SelectedResumeKey);
                resume = resumeRepository.GetById(updatedResumeId);
            }
            else
                resume = new Resume();

            var candidateId = sessionStorage.Get<Guid>(Const.SelectedCandidateKey);
            var candidate = cadidateRepository.GetById(candidateId);

            resumeAddUpdate.UpdateModel(resume);

            if (isUpdate)
            {
                candidateResumeService.Update(updatedResumeId, resume);
                sessionStorage.Remove(Const.SelectedResumeKey);
            }
            else
            {
                var jobTitle = new JobTitle(){Id = resumeAddUpdate.SelectedJobTitle};
                candidateResumeService.Add(candidate, jobTitle, resume);
            }

            LoadCandidateResumes();
            resumeAddUpdate.Visible = false;
        }
    }
}